HAL HSEM Typical use cases for migration ¶
This section provides information about the HAL HSEM Typical use cases for migration.
The HSEM semaphore take/release processes remain unchanged between HAL1 and HAL2.
However, the HAL2 introduces several naming and typing updates. For example, the semaphore identifiers are now defined as typed enumeration values such as
HAL_HSEM_0
to
HAL_HSEM_31, and the semaphore states are returned as typed enumeration values such as
HAL_HSEM_RELEASED
or
HAL_HSEM_TAKEN.
Use case: Take/Release a semaphore ¶
This use case shows how the application take a semaphore before accessing a shared resource, then releases it once the processing is completed.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
uint32_t sem_state;
#define HSEM_SEM_ID (0U) /* HW semaphore 0*/
/* HSEM Clock Enable */
__HAL_RCC_HSEM_CLK_ENABLE() ;
HAL_HSEM_Take(HSEM_SEM_ID, 1U);
sem_state = HAL_HSEM_IsSemTaken(HSEM_SEM_ID);
if (sem_state == 1U)
{
/* The hardware semaphore is taken */
}
HAL_HSEM_Release(HSEM_SEM_ID, 1U);
sem_state = HAL_HSEM_IsSemTaken(HSEM_SEM_ID);
if (sem_state == 0U)
{
/* The hardware semaphore is released */
}
|
hal_hsem_status_t sem_state;
/* HSEM Clock Enable */
HAL_RCC_HSEM_EnableClock();
HAL_HSEM_Take(HAL_HSEM_0, 1U);
sem_state = HAL_HSEM_IsTaken(HAL_HSEM_0);
if (sem_state == HAL_HSEM_TAKEN)
{
/* The hardware semaphore is taken */
}
HAL_HSEM_Release(HAL_HSEM_0, 1U);
sem_state = HAL_HSEM_IsTaken(HAL_HSEM_0);
if (sem_state == HAL_HSEM_RELEASED)
{
/* The hardware semaphore is released */
}
|
Use case: FastTake/ReleaseAll all semaphores at once with correct Key ¶
This use case shows how the application sets a clear key, takes multiple semaphores using the fast-take mechanism, then releases all semaphores associated with the current core in a single operation.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
uint32_t clear_key;
uint32_t sem_state0, sem_state1;
#define HSEM_ID_0 (0U) /* HW semaphore 0*/
#define HSEM_ID_1 (1U) /* HW semaphore 1*/
/* HW semaphore Clock enable */
__HAL_RCC_HSEM_CLK_ENABLE() ;
HAL_HSEM_SetClearKey(clear_key);
HAL_HSEM_FastTake(HSEM_ID_0);
HAL_HSEM_FastTake(HSEM_ID_1);
clear_key = HAL_HSEM_GetClearKey();
HAL_HSEM_ReleaseAll(clear_key, 1U);
sem_state0 = HAL_HSEM_IsSemTaken(HSEM_SEM_0);
sem_state1 = HAL_HSEM_IsSemTaken(HSEM_SEM_1);
if ((sem_state0 == 0U) && (sem_state1 == 0U))
{
/* Both hardware semaphores are released */
}
|
uint32_t clear_key;
hal_hsem_status_t sem_state0, sem_state1;
/* HW semaphore Clock enable */
HAL_RCC_HSEM_EnableClock();
HAL_HSEM_SetClearKey(clear_key);
HAL_HSEM_FastTake(HAL_HSEM_0);
HAL_HSEM_FastTake(HAL_HSEM_1);
clear_key = HAL_HSEM_GetClearKey();
HAL_HSEM_ReleaseAll(clear_key, HAL_HSEM_LOCKID_CURRENT_CPU);
sem_state0 = HAL_HSEM_IsTaken(HAL_HSEM_0);
sem_state1 = HAL_HSEM_IsTaken(HAL_HSEM_1);
if ((sem_state0 == HAL_HSEM_RELEASED) && (sem_state1 == HAL_HSEM_RELEASED))
{
/* Both hardware semaphores are released */
}
|
Use case: Synchronization between two processes ¶
This use case shows how two processes synchronize on the same hardware semaphore. One process owns the semaphore first, while the second one waits for its release through interrupt notification.
Sequence diagram ¶
Code snippet ¶
|
Process |
HAL1 |
HAL2 |
|---|---|---|
|
Process 1 |
#define HSEM_PROCESS_1 12U
#define HSEM_ID 0U /* HW semaphore 0 */
/* HSEM Clock Enable */
__HAL_RCC_HSEM_CLK_ENABLE();
/* Take semaphore 0 with Process 1 */
HAL_HSEM_Take(HSEM_ID, HSEM_PROCESS_1);
/* Release semaphore 0 with Process 1 */
HAL_HSEM_Release(HSEM_ID, HSEM_PROCESS_1);
|
#define HSEM_PROCESS_1 12U
/* HSEM Clock Enable */
HAL_RCC_HSEM_EnableClock();
/* Take semaphore 0 with Process 1 */
HAL_HSEM_Take(HAL_HSEM_0, HSEM_PROCESS_1);
/* Release semaphore 0 with Process 1 */
HAL_HSEM_Release(HAL_HSEM_0, HSEM_PROCESS_1);
|
|
Process 2 |
#define HSEM_PROCESS_2 13U
#define HSEM_ID 0U /* HW semaphore 0 */
static volatile uint32_t hsem_released = 0U;
/* HSEM Clock Enable */
__HAL_RCC_HSEM_CLK_ENABLE();
/* Enable the interruption for semaphore 0 with Process 2 */
HAL_HSEM_ActivateNotification(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID));
/* Wait until the semaphore release is notified */
while (hsem_released == 0U)
{
}
/* Deactivate HSEM notification */
HAL_HSEM_DeactivateNotification(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID));
/* Take semaphore 0 with Process 2 */
HAL_HSEM_Take(HSEM_ID, HSEM_PROCESS_2);
/* Release semaphore 0 with Process 2 */
HAL_HSEM_Release(HSEM_ID, HSEM_PROCESS_2);
void HSEM_IRQHandler(void)
{
HAL_HSEM_IRQHandler();
}
void HAL_HSEM_FreeCallback(uint32_t SemMask)
{
if ((SemMask & __HAL_HSEM_SEMID_TO_MASK(HSEM_ID)) != 0U)
{
hsem_released = 1U;
}
}
|
#define HSEM_PROCESS_2 13U
static volatile uint32_t hsem_released = 0U;
/* HSEM Clock Enable */
HAL_RCC_HSEM_EnableClock();
/* Enable the interruption for semaphore 0 with Process 2 */
HAL_HSEM_EnableFreeNotificationIT(HAL_HSEM_0);
/* Wait until the semaphore release is notified */
while (hsem_released == 0U)
{
}
/* Deactivate HSEM notification */
HAL_HSEM_DisableFreeNotificationIT(HAL_HSEM_0);
/* Take semaphore 0 with Process 2 */
HAL_HSEM_Take(HAL_HSEM_0, HSEM_PROCESS_2);
/* Release semaphore 0 with Process 2 */
HAL_HSEM_Release(HAL_HSEM_0, HSEM_PROCESS_2);
void HSEM_S_IRQHandler(void)
{
HAL_HSEM_IRQHandler();
}
void HAL_HSEM_FreeCallback(hal_hsem_t semaphore)
{
if (semaphore == HAL_HSEM_0)
{
hsem_released = 1U;
}
}
|





